﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

using Microsoft.ApplicationBlocks.Data;

using YouHP.IDAL;
using YouHP.Model;

namespace YouHP.SqlServerDAL
{
    public class GroupNewsDAL : IGroupNews
    {
        #region basic operations
        public bool AddGroupNews(GroupNewsInfo info)
        {
            return SqlHelper.ExecuteNonQuery(Common.ConnStr,
                Common.GroupNewsSP_AddGroupNews,
                info.GroupId,
                info.Title,
                info.Introduction,
                info.LinkUrl,
                info.Body,
                info.UpdateTime,
                info.Source
                ) > 0;
        }

        public bool UpdateGroupNews(GroupNewsInfo info)
        {
            return SqlHelper.ExecuteNonQuery(Common.ConnStr,
                Common.GroupNewsSP_UpdateGroupNews,
                info.Id,
                info.GroupId,
                info.Title,
                info.Introduction,
                info.LinkUrl,
                info.Body,
                info.UpdateTime,
                info.Source
                ) > 0;
        }

        public bool DeleteGroupNews(int[] ids)
        {
            int cnt = ids.Length;
            if (cnt <= 0)
                return false;

            StringBuilder sb = new StringBuilder();
            sb.Append("[id] in (");
            sb.Append(ids[0]);

            for (int i = 1; i < cnt; i++)
            {
                sb.Append(", ");
                sb.Append(ids[i]);
            }
            sb.Append(")");

            return SqlHelper.ExecuteNonQuery(Common.ConnStr,
                Common.GroupNewsSP_DelGroupNews,
                sb.ToString()) >= cnt;
        }

        public GroupNewsInfo GetGroupNewsInfo(int id)
        {
            IDataReader reader = SqlHelper.ExecuteReader(Common.ConnStr,
                Common.GroupNewsSP_GetGroupNews,
                id) as IDataReader;
            return CBO.FillObject<GroupNewsInfo>(reader);
        }
        #endregion

        #region list and search operations
        private List<GroupNewsInfo> GetGroupNewsList(int pageSize, int pageIndex, string condStr, string orderStr, out int count)
        {
            DataSet dset = SqlHelper.ExecuteDataset(Common.ConnStr,
                Common.GetFullName(Common.GroupNewsSP_GetGroupNewsList),
                pageSize, pageIndex, condStr, orderStr);

            IDataReader dr = dset.Tables[0].CreateDataReader();
            if (dr != null && dr.Read())
            {
                count = dr.GetInt32(0);
                dr.Close();
            }
            else
            {
                count = 0;
            }
            dr = dset.Tables[1].CreateDataReader();

            return CBO.FillCollection<GroupNewsInfo>(dr);
        }

        private List<GroupNewsInfo> GetTopGroupNews(string condStr, string orderStr, int n)
        {
            IDataReader reader = SqlHelper.ExecuteReader(Common.ConnStr,
                Common.GroupNewsSP_GetTopGroupNews,
                n, condStr, orderStr) as IDataReader;
            return CBO.FillCollection<GroupNewsInfo>(reader);
        }

        public List<GroupNewsInfo> GetGroupNewsList(int pageSize, int pageIndex, out int count)
        {
            return GetGroupNewsList(pageSize, pageIndex, "", " [Id] desc ", out count);
        }

        public List<GroupNewsInfo> GetGroupNewsByName(int pageSize, int pageIndex, string keyword, out int count)
        {
            string condStr = string.Format(@" [Title] like '%{0}%' ", keyword);
            return GetGroupNewsList(pageSize, pageIndex, condStr, " [GroupNews].[Id] desc ", out count);
        }

        public List<GroupNewsInfo> GetGroupNewsByGroupId(int pageSize, int pageIndex, int groupId, out int count)
        {
            string condStr = string.Format(@" [GroupId]={0} ", groupId);
            return GetGroupNewsList(pageSize, pageIndex, condStr, " [GroupNews].[Id] desc ", out count);
        }

        // 取得某个圈子最新的n条新闻
        public List<GroupNewsInfo> GetLattestNews(int groupId, int n)
        {
            string condStr = string.Format(@" [GroupId] = {0} ", groupId);
            return GetTopGroupNews(condStr, " [GroupNews].[Id] desc ", n);
        }
        #endregion
    }
}

